package com.hilaver.dzmis.web.servlet.basicinformation;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiFranceCustomer;
import com.hilaver.dzmis.basicinfo.BiFranceCustomerVisitMemo;
import com.hilaver.dzmis.service.impl.BiFranceCustomerServiceImpl;
import com.hilaver.dzmis.web.servlet.AbstractBaseServlet;

public class BiFranceCustomerServlet extends AbstractBaseServlet{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7229707318140282893L;
	
	public final static int ACTION_EDIT_VISIT_MEMO = 101;
	public final static int ACTION_DELETE_VISIT_MEMO = 102; // "delete";
	public final static int ACTION_GET_VISIT_MEMO = 103; // "get one";
	public final static int ACTION_GET_ALL_VISIT_MEMO_PAGINATION_FILTER = 104; 

	public BiFranceCustomerServlet(){
		this.fullName = BiFranceCustomer.class.getName();
		this.simpleName = BiFranceCustomer.class.getSimpleName();
	}
	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			writer.write(execute(request));
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);

		BiFranceCustomerServiceImpl serviceObj = new BiFranceCustomerServiceImpl();
		Method method = null;
		String rtn = "";
		Locale locale = (Locale) request.getSession().getAttribute(
				Constants.SESSION_LOCALE);
		
		method = serviceObj.getClass().getMethod("setLocale",
				new Class[] { Locale.class});
		method.invoke(serviceObj, new Object[] { locale });
		
		super.setAcUser(request, serviceObj);
		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";
			this.setPaginationParam(request);
			method = serviceObj.getClass().getMethod("getAllPagination",
					new Class[] { int.class, String.class, String.class });// int
			// page,
			// String
			// sort,
			// String
			// order
			rtn = (String) method.invoke(serviceObj, new Object[] { this.page,
					this.sort, this.dir });
			break;
		case ACTION_EDIT: // "edit";
			this.fillObj(request);
			method = serviceObj.getClass().getMethod("edit",
					new Class[] { Object.class });
			rtn = (String) method.invoke(serviceObj, new Object[] { this.obj });
			break;
		case ACTION_DELETE: // "delete";
			this.setId(request);
			method = serviceObj.getClass().getMethod("delete",
					new Class[] { int.class });
			rtn = (String) method.invoke(serviceObj, new Object[] { this.id });
			break;
		case ACTION_GET: // "get one"
			this.setId(request);
			method = serviceObj.getClass().getMethod("get",
					new Class[] { int.class });
			rtn = (String) method.invoke(serviceObj, new Object[] { this.id });
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"
			this.setPaginationParam(request);
			this.setFilterParam(request);
			method = serviceObj.getClass().getMethod(
					"getAllPagination",
					new Class[] { int.class, String.class, String.class,
							String[].class });// int page, String sort, String
			// order
			rtn = (String) method.invoke(serviceObj, new Object[] { this.page,
					this.sort, this.dir, this.filters });
			break;
		case ACTION_GET_ALL:
			this.setPaginationParam(request);
			method = serviceObj.getClass().getMethod("getAll",
					new Class[] { String.class, String.class });// String sort,
																// String
			// order
			rtn = (String) method.invoke(serviceObj, new Object[] { this.sort,
					this.dir });
			break;
		case ACTION_EDIT_VISIT_MEMO:
			this.fillVisitMemo(request);
			rtn = serviceObj.edit(this.obj);
			break;
		case ACTION_DELETE_VISIT_MEMO:
			this.setId(request);
			rtn = serviceObj.deleteVisitMemo(this.id);
			break;
		case ACTION_GET_VISIT_MEMO:
			this.setId(request);
			rtn = serviceObj.getVisitMemo(this.id);
			break;
		case ACTION_GET_ALL_VISIT_MEMO_PAGINATION_FILTER:
			this.setId(request);
			this.setPaginationParam(request);
			this.setFilterParam(request);
			rtn = serviceObj.getAllVisitMemoPagination(this.id, this.page, this.sort, this.dir, this.filters);
			break;
		}

		return rtn;
	}
	
	private void fillVisitMemo(HttpServletRequest request) throws Exception{
		Integer fcId = Integer.parseInt(request.getParameter("fcId"));
		BiFranceCustomer bfc = new BiFranceCustomer();
		bfc.setId(fcId);
		super.fillObj(BiFranceCustomerVisitMemo.class.getName(), request);
		((BiFranceCustomerVisitMemo)this.obj).setBiFranceCustomer(bfc);
	}

}
